package cn.wolfcode.wolf2w.listener;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyStatisVoRedisService;
import cn.wolfcode.wolf2w.vo.StrategyStatisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

@Component//组件
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
//上下文更新事件（ContextRefreshedEvent）：在调用ConfigurableApplicationContext 接口中的refresh()方法时被触发

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyStatisVoRedisService strategyStatisVoRedisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("--------------初始化开始了--------------");
        //现在查出所有的攻略
        //在真实开发中并不会直接初始化所有的数据,那样会造成内存的溢出
        //通常只挑选重要的数据进行初始化
        List<Strategy> strategies = strategyService.list();

        //遍历攻略list,把对应的vo设置进redis做数据的初始化
        for (Strategy strategy : strategies) {
            //获取攻略对应的vo
            StrategyStatisVo vo = strategyStatisVoRedisService.getStrategyStatisVo(strategy.getId());
            //设置进redis中
            strategyStatisVoRedisService.setStrategyStatisVo(vo);
        }
        System.out.println("--------------初始化结束了--------------");
    }
}
